﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using PromoServer.Library.BLL;
using PromoServer.Library.Entities;

namespace PromoServer_WPF
{
    /// <summary>
    /// Interaction logic for ScheduleEditorWindow.xaml
    /// </summary>
    public partial class ScheduleEditorWindow : Window
    {
        PSSchedule psSchedule;
        private bool isNewSchedule;

        public ScheduleEditorWindow(bool isNewSchedule)
        {
            InitializeComponent();

            psSchedule = new PSSchedule();

            if (isNewSchedule)
                loadNewSchedule();
            else
                loadSavedSchedules();

            bindDropdownSelections();
        }

        private void bindDropdownSelections()
        {
            //this.startPeriodComboBox.ItemsSource = psSchedule.ValidPeriods;
            //this.endPeriodComboBox.ItemsSource = psSchedule.ValidPeriods;
        }

        private void loadNewSchedule()
        {
            isNewSchedule = true;

            Schedule newSchedule = new Schedule("[Enter Schedule Name]");

            // Bind controls
            nameTextGrid.DataContext = newSchedule;

            mondayGrid.DataContext = newSchedule;
            tuesdayGrid.DataContext = newSchedule;
            wednesdayGrid.DataContext = newSchedule;
            thursdayGrid.DataContext = newSchedule;
            fridayGrid.DataContext = newSchedule;
            saturdayGrid.DataContext = newSchedule;
            sundayGrid.DataContext = newSchedule;

            startHourGrid.DataContext = newSchedule;
            startMinuteGrid.DataContext = newSchedule;
            startPeriodGrid.DataContext = newSchedule;

            endHourGrid.DataContext = newSchedule;
            endMinuteGrid.DataContext = newSchedule;
            endPeriodGrid.DataContext = newSchedule;

            startDateGrid.DataContext = newSchedule;
            endDateGrid.DataContext = newSchedule;

            summaryGrid.DataContext = newSchedule;

            // Set controls
            nameComboBox.Visibility = Visibility.Hidden;
            nameTextBox.Visibility = Visibility.Visible;
            cancelButton.Visibility = Visibility.Visible;
            newButton.Visibility = Visibility.Hidden;
        }

        private void loadSavedSchedules()
        {
            isNewSchedule = false;

            CollectionViewSource scheduleViewSource = ((CollectionViewSource)(this.FindResource("scheduleViewSource")));
            scheduleViewSource.Source = psSchedule.GetSchedules();

            // Bind controls
            mondayGrid.DataContext = (CollectionViewSource)(this.FindResource("scheduleViewSource"));
            tuesdayGrid.DataContext = (CollectionViewSource)(this.FindResource("scheduleViewSource"));
            wednesdayGrid.DataContext = (CollectionViewSource)(this.FindResource("scheduleViewSource"));
            thursdayGrid.DataContext = (CollectionViewSource)(this.FindResource("scheduleViewSource"));
            fridayGrid.DataContext = (CollectionViewSource)(this.FindResource("scheduleViewSource"));
            saturdayGrid.DataContext = (CollectionViewSource)(this.FindResource("scheduleViewSource"));
            sundayGrid.DataContext = (CollectionViewSource)(this.FindResource("scheduleViewSource"));

            startHourGrid.DataContext = (CollectionViewSource)(this.FindResource("scheduleViewSource"));
            startMinuteGrid.DataContext = (CollectionViewSource)(this.FindResource("scheduleViewSource"));
            startPeriodGrid.DataContext = (CollectionViewSource)(this.FindResource("scheduleViewSource"));

            endHourGrid.DataContext = (CollectionViewSource)(this.FindResource("scheduleViewSource"));
            endMinuteGrid.DataContext = (CollectionViewSource)(this.FindResource("scheduleViewSource"));
            endPeriodGrid.DataContext = (CollectionViewSource)(this.FindResource("scheduleViewSource"));

            startDateGrid.DataContext = (CollectionViewSource)(this.FindResource("scheduleViewSource"));
            endDateGrid.DataContext = (CollectionViewSource)(this.FindResource("scheduleViewSource"));

            summaryGrid.DataContext = (CollectionViewSource)(this.FindResource("scheduleViewSource"));

            // Set controls
            nameComboBox.Visibility = Visibility.Visible;
            nameTextBox.Visibility = Visibility.Hidden;
            cancelButton.Visibility = Visibility.Hidden;
            newButton.Visibility = Visibility.Visible;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            loadSavedSchedules();
        }

        private void newButton_Click(object sender, RoutedEventArgs e)
        {
            loadNewSchedule();
        }

        private void exitButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void saveButton_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
